import 'dart:convert';
import 'package:flutter_module/common/http/YZHttp.dart';
import 'package:flutter_module/model/orgainize.dart';
import 'package:flutter_boost/flutter_boost.dart';
import '../../../common/base/logic/BasicListLogic.dart';
import '../../../common/http/ApiConstant.dart';
import '../../../common/http/YZHttpUtils.dart';
import '../../../common/third/analytics/YZAnalytics.dart';
import 'search_colleague_state.dart';

/// @description:
/// @author
/// @date: 2022-12-30 15:23:31
class SearchColleagueLogic extends BasicListLogic {
  final state = SearchColleagueState();

  @override
  void onInit() {
    super.onInit();
    requestUserList(true, configNetWorkParmas());
    state.controller.addListener(() {});
    YZAnalytics.track("Z00001", "点赞-添加点赞同事", "上进赞", null);
  }

  @override
  void dispose() {
    state.controller.removeListener(() {});
    super.dispose();
  }

  void requestUserList(bool isHeader, Map<String, dynamic>? info) {
    HttpUtils.get(
        state.pageType == 0
            ? api_userInfo_historycallyLike
            : api_userInfo_departmentInfoList,
        params: info, successCallBack: (data, total) {
      dealUserListDatas(data, isHeader, total);
    }, errorCallBack: (e) {
      failRefresh(isHeader, e);
    });
  }

  void dealUserListDatas(dynamic data, bool isHeader, int total) {
    List<Orgainize> items = data == null
        ? []
        : (data as List).map((e) => Orgainize.fromJson(e)).toList();
    if (isHeader) {
      dataList = items;
    } else {
      dataList.addAll(items);
    }
    successRefresh(isHeader, items.length);
  }

  void selectBtnClicked(Orgainize user) {
    print('${user.userName}');
    state.userFocusNode.unfocus();
    String userStr = jsonEncode(user.toJson());
    print('userStr===$userStr');
    BoostNavigator.instance.pop({'selectLikeUser': userStr});
    // YouthChannel.instance.sendEventToNative(CHANNEL_EVENT_MESSAGE, {
    //   KEY_MESSAGE_CONTENT: {'selectLikeUser': userStr},
    // });
  }

  void clearSearchText() {
    page = 1;
    state.controller.clear();
    state.searchText = '';
    state.isShowClearBtn = false;
    requestUserList(true, configNetWorkParmas());
  }

  void completeInput(String text) {
    page = 1;
    state.searchText = text;
    state.isShowClearBtn = text.isNotEmpty;
    requestUserList(true, configNetWorkParmas());
  }

  @override
  Map<String, dynamic>? configNetWorkParmas() {
    // SharedPreferences.instance.user
    return state.pageType == 2
        ?
        // 公司 - 2
        {'page': page, 'pageSize': PAGE_SIZE, 'orgUserName': state.searchText}
        : (state.pageType == 1
            ?
            // 部门 - 1
            {
                'page': page,
                'pageSize': PAGE_SIZE,
                'orgUserName': state.searchText,
                'orgSignificantDepartmentId': state.orgSignificantDepartmentId
              }
            :
            // 历史点赞 - 0
            {
                'page': page,
                'pageSize': PAGE_SIZE,
                'orgUserName': state.searchText,
                'orgSignificantDepartmentId': state.orgSignificantDepartmentId,
              });
  }

  @override
  void footerRetryRefresh() {
    refreshController.requestLoading();
  }

  @override
  void getnetworkdata(bool isHeaderRefresh, Map<String, dynamic>? info) {
    requestUserList(isHeaderRefresh, configNetWorkParmas());
  }
}
